package com.dmzc.stage1.chapter3;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.*;

public class FlightQueryExample {
    private static List<String>flightCompany= Arrays.asList("CSA","CEA","HNA");

    public static void main(String[] args) {
        List<String>results=search("SH","BJ");
        System.out.println("==========result=========");
        results.forEach(System.out::println);
    }

    private static List<String> search(String original, String dest) {
        final List<String>result=new ArrayList<>();
        List<FlightQueryTask>tasks=flightCompany.stream()
                .map(f->createSearchTask(f,original,dest))
                .collect(toList());
        tasks.forEach(Thread::start);
        //已经start，和之后的join并行，是否会出现问题
        tasks.forEach(t->{
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        tasks.stream().map(FightQuery::get).forEach(result::addAll);
        return result;
    }
    private static FlightQueryTask createSearchTask(
            String flight,
            String original,String dest)
    {
        FlightQueryTask t=new FlightQueryTask(flight,original,dest);
        return  t;

    }

}
